package com.ruoyi.web.service.Impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.web.domain.MesAllotDetail;
import com.ruoyi.web.domain.MesAllotInfo;
import com.ruoyi.web.domain.mes.MesPaper;
import com.ruoyi.web.service.ProducePrintService;
import com.ruoyi.web.vo.Produce;
import com.ruoyi.web.vo.ProducePrint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.domain.MesMakeversionDetail;
import com.ruoyi.web.mapper.MesMakeVersionMapper;
import com.ruoyi.web.domain.MesMakeVersion;
import com.ruoyi.web.service.IMesMakeVersionService;

import javax.annotation.Resource;

/**
 * 做版管理Service业务层处理
 * 
 * @author wxt
 * @date 2024-02-02
 */
@Service
public class MesMakeVersionServiceImpl implements IMesMakeVersionService 
{
    @Autowired
    private MesMakeVersionMapper mesMakeVersionMapper;

    @Resource
    private ProducePrintService producePrintService;
    /**
     * 查询做版管理
     * 
     * @param id 做版管理主键
     * @return 做版管理
     */
    @Override
    public MesMakeVersion selectMesMakeVersionById(Long id)
    {
        MesMakeVersion data= mesMakeVersionMapper.selectMesMakeVersionById(id);
        return data;
    }

    /**
     * 查询做版管理列表
     * 
     * @param mesMakeVersion 做版管理
     * @return 做版管理
     */
    @Override
    public List<MesMakeVersion> selectMesMakeVersionList(MesMakeVersion mesMakeVersion)
    {
        return mesMakeVersionMapper.selectMesMakeVersionList(mesMakeVersion);
    }
    @Override
    public List<MesMakeversionDetail> getDetailList(MesMakeversionDetail mesMakeversionDetail)
    {
        List<MesMakeversionDetail> data= mesMakeVersionMapper.getDetailList(mesMakeversionDetail);
//        for (MesMakeversionDetail list:data) {
//
//            list.setSheetTotal();
//        }
        return data;
    }



    /**
     * 新增做版管理
     * 
     * @param mesMakeVersion 做版管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesMakeVersion(MesMakeVersion mesMakeVersion)
    {
        mesMakeVersion.setCreateTime(DateUtils.getNowDate());

        // 生成做版单编号
        int maxNum = mesMakeVersionMapper.selectMaxId();
        String code = PinYinUtils.getCodeNormal(maxNum, "ZB");
        mesMakeVersion.setMakeCode(code);

        int rows = mesMakeVersionMapper.insertMesMakeVersion(mesMakeVersion);
        insertMesMakeversionDetail(mesMakeVersion);
        return rows;
    }

    /**
     * 修改做版管理
     * 
     * @param mesMakeVersion 做版管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesMakeVersion(MesMakeVersion mesMakeVersion)
    {
        if (mesMakeVersion.getStatus()==2)//提交
             {
                 MesMakeVersion Info= mesMakeVersionMapper.selectMesMakeVersionById(mesMakeVersion.getId());//查询数据库
                 List<MesMakeversionDetail> mesDetailList = Info.getMesMakeversionDetailList();
                 if (mesDetailList.size()!=0) {
                     List<String> ProducePrintId = mesDetailList.stream().map(it -> it.getProducePrintId()).collect(Collectors.toList());
                     List<String> ProduceId=  mesDetailList.stream().map(MesMakeversionDetail::getProduceId).distinct().collect(Collectors.toList());
                     String[] ProducePrintIds = ProducePrintId.toArray(new String[0]);//印刷单id

                     MesMakeversionDetail makede=new MesMakeversionDetail();
//                     makede.setProducePrintIds(ProducePrintIds);//生产印刷单集合
//                     makede.setMakeStatus("1");//生产印刷单状态

                     //查询是否做版
//                     int ismake = mesMakeVersionMapper.getismakes(makede);
//                     if (ismake>0){
//                         throw new ServiceException("部分印刷单已提交做版，不可重复提交");
//                     }
                     //批量修改状态为做版
//                      mesMakeVersionMapper.updateismakes(makede);
                     for (String produceid:ProduceId){
                         makede.setProduceId(produceid);//生产单id
//                         makede.setMakeStatus("1");//生产印刷单状态
//                         int count = mesMakeVersionMapper.getroducesismake(makede);//查询生产单下印刷单没有做版的数量
//                         if (count==0){//修改生产单状态
                             makede.setMakeStatus("3");//生产单状态(已做版)
                              mesMakeVersionMapper.updateproducestatus(makede);
//                         }
                     }
                 }
             }else{
            mesMakeVersionMapper.deleteMesMakeversionDetailByMakeVersionId(mesMakeVersion.getId());
            insertMesMakeversionDetail(mesMakeVersion);
        }
        return mesMakeVersionMapper.updateMesMakeVersion(mesMakeVersion);
    }

    /**
     * 批量删除做版管理
     * 
     * @param ids 需要删除的做版管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesMakeVersionByIds(Long[] ids)
    {
        mesMakeVersionMapper.deleteMesMakeversionDetailByMakeVersionIds(ids);
        return mesMakeVersionMapper.deleteMesMakeVersionByIds(ids);
    }

    /**
     * 删除做版管理信息
     * 
     * @param id 做版管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesMakeVersionById(Long id)
    {
        mesMakeVersionMapper.deleteMesMakeversionDetailByMakeVersionId(id);
        return mesMakeVersionMapper.deleteMesMakeVersionById(id);
    }

    /**
     * 新增做版详情信息
     * 
     * @param mesMakeVersion 做版管理对象
     */
    public void insertMesMakeversionDetail(MesMakeVersion mesMakeVersion)
    {
        List<MesMakeversionDetail> mesMakeversionDetailList = mesMakeVersion.getMesMakeversionDetailList();
        Long id = mesMakeVersion.getId();
        if (StringUtils.isNotNull(mesMakeversionDetailList))
        {
            List<MesMakeversionDetail> list = new ArrayList<MesMakeversionDetail>();
            int xh=0;
            for (MesMakeversionDetail mesMakeversionDetail : mesMakeversionDetailList)
            {
                mesMakeversionDetail.setMakeVersionId(id);
                list.add(mesMakeversionDetail);
                //做版序列号
                xh = xh + 1;
                String xlh = PinYinUtils.hundred(xh);//序列号
                mesMakeversionDetail.setMakeNo(mesMakeVersion.getMakeCode() + "ZB" + xlh);

                String printid=mesMakeversionDetail.getProducePrintId();
                //更改生产印刷单是否做版
                producePrintService.update(new UpdateWrapper<ProducePrint>()
                        .lambda().set(ProducePrint::getIsMake,"2").eq(ProducePrint::getPrintId,printid));
            }
            if (list.size() > 0)
            {
                mesMakeVersionMapper.batchMesMakeversionDetail(list);
            }
        }
    }
}
